<?php

namespace app\manage\controller;

use think\Db;
use think\Request;
use app\model\ChargeModel;
class Charge extends Base
{
    protected $request;
    protected $model;
    public function __construct(Request $request)
    {
        parent::__construct();
		$this->request = $request;
        $this->model = new ChargeModel();
        $this->assign("data",$this->request->param());
   
    }
    //创建缴费记录
    public function addCost(){
        
        $data = $this->model->add($this->request->param());

        return ['msg'=>$data['msg'],'code'=>$data['code']];
        
    }
    //缴费列表
    public function list(){

        $list = $this->model->alias('a')->order("a.id",'desc')->where(function($query){
            //等级
            $level = $this->request->param('admin_auth')['level'];
            if($level == 4){//业务员
                $query->where("b.account_id",$this->request->param('admin_auth')['auth_admin_id']);
            }
            if($level != 1 && $level != 3){
                //公司id
                $cid = $this->request->param('admin_auth')['cid'];
                $query->where("b.cid",$cid);
            }
            $serial_number = input("serial_number");
            if(!empty($serial_number)){
                $query->where("a.students_sn",$serial_number);
            }
            $user_name = input("user_name");
            if(!empty($user_name)){
                $query->where("b.user_name|b.phone",'like','%'.$user_name.'%');
            }
            //业务员
            $nickname = input("nickname");
            if(!empty($nickname)){
                $query->where("b.account_name",'like','%'.$nickname.'%');
            }
            //公司名称
            $company_name = input("company_name");
            if(!empty($company_name)){
                $query->where("b.company_name",'like','%'.$company_name.'%');
            }
            $school_name = input("school_name");
            if(!empty($school_name)){
                $query->where("b.school_name",'like','%'.$school_name.'%');
            }
            $status = input("status");
            if(!empty($status)){
                $query->where("a.status",$status);
            }
            //创建时间区间
            $start = input("start",'');
            $end = input("end",'');
            if(!empty($start) && !empty($end)){
                $query->where("a.create_time",'between',[$start,$end]);
            }
        })
        ->join("students b",'a.students_sn = b.order_sn')
        ->field('a.id,a.account_name,a.students_sn,b.company_name,a.total_audit_name,a.total_update_time,a.audit_id,a.agreement_price,a.submit_price,a.overdue_price,a.status,a.audit_name,a.create_time,a.update_time,b.user_name,b.school_name,b.type,b.hierarchy')
        ->paginate(15,false,['query' =>$this->request->param()]);
        $page = $list->render();
        $this->assign("list",$list);
        $this->assign('page', $page);
        return view("student/list");
    }
    //添加缴费页面
    public function supplement(){
       
        //$info = $this->model->order("id",'desc')->where('students_sn',$this->request->param('id'))->find();
        $info = Db::name("balance")->where('students_sn',$this->request->param('id'))->find();
        if(abs($info['agreement_price']) == 0){
            $info['overdue_price'] = ChargeModel::agreementPrice($info['students_sn']);
            $info['agreement_price'] = $info['overdue_price'];
        }
        
        $this->assign("info",$info);

        return view("student/supplement");
    } 
    //补交处理
    public function ajaxSupplement(){
      
        $data = $this->model->supplementAdd($this->request->param());

        return ['msg'=>$data['msg'],'code'=>$data['code']];
    }
    //分公司审核
    public function ajaxAudit(){

        $data = $this->model->financialAudit($this->request->param());

        return ['msg'=>$data['msg'],'code'=>$data['code']];
    }
    //总公司审核
    public function ajaxtotalAudit(){
        $data = $this->model->financialTotalAudit($this->request->param());

        return ['msg'=>$data['msg'],'code'=>$data['code']];
    }
    










}